package zgrjb.snbq.teach_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import zgrjb.snbq.teach_platform.component.OSSComponent;
import zgrjb.snbq.teach_platform.dto.model.ClassDO;
import zgrjb.snbq.teach_platform.dto.model.FileDO;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.dto.req.AddFileRequest;
import zgrjb.snbq.teach_platform.dto.req.CreateClassRequest;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.mapper.ClassMapper;
import zgrjb.snbq.teach_platform.mapper.FileMapper;
import zgrjb.snbq.teach_platform.service.ClassService;
import zgrjb.snbq.teach_platform.util.IDUtil;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private OSSComponent ossComponent;

    @Override
    @CacheEvict(value = "classList", allEntries = true) // 创建后清除所有班级列表缓存
    public JsonData create(CreateClassRequest request) {
        User user = LoginInterceptor.getCurrentUser();
        if (user == null) {
            return JsonData.buildError("用户未登录");
        }
        
        ClassDO classDO = new ClassDO();
        classDO.setId((Long) IDUtil.geneSnowFlakeID());
        classDO.setName(request.getName());
        classDO.setDescription(request.getDescription());
        classDO.setSubject(request.getSubject());
        classDO.setTeacherId(user.getId());
        classDO.setTeacherName(user.getName());
        classDO.setTeacherPhone(user.getPhone());
        classDO.setMaxStudents(50); // 默认最大学生数
        classDO.setCurrentStudents(0); // 初始学生数为0
        classDO.setStatus(1); // 默认正常状态
        
        classMapper.insert(classDO);
        return JsonData.buildSuccess();
    }

    @Override
    @CacheEvict(value = {"classList", "fileList"}, allEntries = true) // 删除后清除相关缓存
    public JsonData delete(Long id) {
        classMapper.deleteById(id);
        return JsonData.buildSuccess();
    }

    @Override
    @Cacheable(value = "classList", key = "'teacher_' + #root.target.getCurrentUserId()")
    public JsonData getList() {
        User user = LoginInterceptor.getCurrentUser();
        if (user == null) {
            return JsonData.buildError("用户未登录");
        }
        
        List<ClassDO> classes = classMapper.selectList(new QueryWrapper<ClassDO>()
                .eq("teacher_id", user.getId())
                .eq("status", 1) // 只查询正常状态的班级
                .orderByDesc("create_time"));
        return JsonData.buildSuccess(classes);
    }

    @Override
    @Cacheable(value = "classList", key = "'class_' + #id")
    public JsonData info(Long id) {
        ClassDO classDO = classMapper.selectOne(new QueryWrapper<ClassDO>()
                .eq("id", id)
                .eq("status", 1));
        return JsonData.buildSuccess(classDO);
    }

    @Override
    public JsonData uploadFile(MultipartFile file) {
        return ossComponent.uploadFile(file);
    }
    
    /**
     * 获取当前用户ID，用于缓存key
     */
    public Long getCurrentUserId() {
        User user = LoginInterceptor.getCurrentUser();
        return user != null ? user.getId() : 0L;
    }
}
